unit settings;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  ComCtrls, ExtCtrls, StdCtrls;

type

  { TfrmSettings }

  TfrmSettings = class(TForm)
    btnRegenerateNewdb: TBitBtn;
    btnDBtoDevelop: TBitBtn;
    btnClearAllGoodsCount: TBitBtn;
    btnOptimizeDB: TBitBtn;
    btnSave: TBitBtn;
    btnClose: TBitBtn;
    lblEdtDBArchiveDaysReminder: TLabeledEdit;
    lblInfo: TLabel;
    lbledtExchangeRate: TLabeledEdit;
    memName: TMemo;
    PageControl: TPageControl;
    TabSheetDB: TTabSheet;
    TabSheetGeneral: TTabSheet;
    procedure btnClearAllGoodsCountClick(Sender: TObject);
    procedure btnDBtoDevelopClick(Sender: TObject);
    procedure btnOptimizeDBClick(Sender: TObject);
    procedure btnRegenerateNewdbClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure checkErrors(Sender: TObject);
    procedure lbledtExchangeRateKeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmSettings: TfrmSettings;

implementation

{$R *.lfm}

{ TfrmSettings }

uses main,ZDataset,db,DbCtrls;

procedure TfrmSettings.checkErrors(Sender: TObject);
begin
  if ((lbledtExchangeRate.Text=EmptyStr)) then begin
    btnSave.Enabled:=False;
  end else begin
    btnSave.Enabled:=True;
  end;
end;

procedure TfrmSettings.lbledtExchangeRateKeyPress(Sender: TObject; var Key: char);
begin
  FrmMain.KeyVerifyFloat(Sender,Key);
end;

procedure TfrmSettings.btnSaveClick(Sender: TObject);
begin
  frmMain.execQuery('UPDATE settings SET value='+lbledtExchangeRate.Text+' WHERE id=2');
  frmMain.ExchangeRate:=StrToFloat(lbledtExchangeRate.Text);
  frmMain.execQuery('UPDATE settings SET value='+QuotedStr(memName.Lines.Text)+' WHERE id=3');
  frmMain.entrepreneur:=memName.Lines.Text;
end;

procedure TfrmSettings.btnClearAllGoodsCountClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Встановити кількість в номенклатурі та на складах в "0" ?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    frmMain.execQuery('UPDATE nomenclatureonstorehouses SET quantity=0');
    frmMain.execQuery('UPDATE nomenclature SET quantity=0');
  end;
end;

procedure TfrmSettings.btnDBtoDevelopClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Замістити існуючі дані вигаданими/режим розробки?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    frmMain.execQuery('UPDATE settings SET value='+QuotedStr('Ваше прізвище ім''я по батькові'+#13+'моб. 068XXX8855'+#13+'mymail-address@i.ua'+#13+'skype: xxx_xxxiua')+' WHERE id=3');
    frmMain.execQuery('UPDATE counterparties SET info='+QuotedStr('Додаткова інформація'+#13+'моб. 068X3X8855'+#13+'some-address@mail.ru'+#13+'skype: xxx_xxxnru')+' WHERE isgroup="N"');
    frmMain.execQuery('UPDATE nomenclature SET price=price+6.27,priceretail=priceretail+16,pricesmallwholesale=pricesmallwholesale+12,pricebigwholesale=pricebigwholesale+6 WHERE isgroup="N"');
  end;
end;

procedure TfrmSettings.btnOptimizeDBClick(Sender: TObject);
begin
  frmMain.execQuery('VACUUM pictures;');
  frmMain.execQuery('VACUUM nomenclature;');
  frmMain.execQuery('VACUUM counterparties;');
  frmMain.execQuery('VACUUM comingsgoods;');
  frmMain.execQuery('VACUUM outputgoods;');
  frmMain.execQuery('VACUUM comingsofinvoicesongoods;');
  frmMain.execQuery('VACUUM outputofinvoicesongoods;');
  frmMain.execQuery('VACUUM settings;');
  frmMain.execQuery('VACUUM history;');
  frmMain.execQuery('VACUUM storehouses;');
  frmMain.execQuery('VACUUM nomenclatureonstorehouses;');
end;

procedure TfrmSettings.btnRegenerateNewdbClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Очистити базу даних та створити демо позиції?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
  frmMain.execQuery('DROP TABLE comingsgoods; ' +
  'CREATE TABLE comingsgoods (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,invoiceid integer,nomenclatureid integer,quantity integer,price real);');
  frmMain.execQuery('DROP TABLE comingsofinvoicesongoods; '+
  'CREATE TABLE comingsofinvoicesongoods (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"date" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,invoiceid varchar(80),counterpartyid integer,storehouse integer);');
  frmMain.execQuery('DROP TABLE counterparties; '+
  'CREATE TABLE counterparties (id integer PRIMARY KEY NOT NULL,title varchar(200) NOT NULL,isgroup boolean NOT NULL,parentid integer NOT NULL,info text);');
  frmMain.execQuery('DROP TABLE history; '+
  'CREATE TABLE history (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"date" datetime DEFAULT CURRENT_TIMESTAMP,title text,info text);');
  frmMain.execQuery('DROP TABLE nomenclature; '+
  'CREATE TABLE nomenclature (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,isgroup boolean NOT NULL,parentid integer NOT NULL,title varchar(200) NOT NULL,articleofgoods varchar(88),articleinside varchar(88),price real,priceretail integer,pricesmallwholesale integer,pricebigwholesale integer,quantity integer DEFAULT 0,quantitymin integer DEFAULT 0);');
  frmMain.execQuery('DROP TABLE nomenclatureonstorehouses;'+
  'CREATE TABLE nomenclatureonstorehouses (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,storehouseid integer,nomenclatureid integer,quantity integer); '+
  'CREATE UNIQUE INDEX nomenclatureonstorehouses_Index ON nomenclatureonstorehouses (id);');
  frmMain.execQuery('DROP TABLE outputgoods; '+
  'CREATE TABLE outputgoods (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,invoiceid integer,nomenclatureid integer,typeofprice integer,quantity integer,price real);');
  frmMain.execQuery('DROP TABLE outputofinvoicesongoods; '+
  'CREATE TABLE outputofinvoicesongoods (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"date" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,counterpartyid integer,storehouse integer);');
  frmMain.execQuery('DROP TABLE pictures; '+
  'CREATE TABLE pictures (id integer PRIMARY KEY AUTOINCREMENT NOT NULL, nomenclatureid integer,data blob);');
  frmMain.execQuery('DROP TABLE storehouses; '+
  'CREATE TABLE storehouses (id integer PRIMARY KEY AUTOINCREMENT NOT NULL,title varchar(150) NOT NULL);');
  frmMain.execQuery('INSERT INTO storehouses VALUES(NULL,"Склад гараж")');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Пральні машини",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Двері",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Мобільні телефони",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Автозапчастини",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Мотозапчастини",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Буд матеріали",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,0,"Скутери",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,1,7,"Honda DIO",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)');
  frmMain.execQuery('INSERT INTO nomenclature VALUES(NULL,0,8,"Honda DIO 24","AACDX74D","674D",678,80,60,40,7,6)');
  frmMain.execQuery('INSERT INTO nomenclatureonstorehouses VALUES(NULL,1,9,7)');
  frmMain.execQuery('INSERT INTO counterparties VALUES(NULL,"Постачальники",1,0,NULL)');
  frmMain.execQuery('INSERT INTO counterparties VALUES(NULL,"Клієнти",1,0,NULL)');
  frmMain.execQuery('INSERT INTO counterparties VALUES(NULL,"Київ",1,1,NULL)');
  frmMain.execQuery('INSERT INTO counterparties VALUES(NULL,"Одеса",1,1,NULL)');
  end;
end;

end.

